package com.yangxk.kadmin.web.module.system.controller;

import com.yangxk.kadmin.common.beans.Page;
import com.yangxk.kadmin.common.beans.Result;
import com.yangxk.kadmin.common.exception.BizException;
import com.yangxk.kadmin.web.common.enumeration.ResponseCode;
import com.yangxk.kadmin.web.module.system.form.*;
import com.yangxk.kadmin.web.module.system.po.User;
import com.yangxk.kadmin.web.module.system.service.UserService;
import com.yangxk.kadmin.web.module.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public Result<Page<UserVo>> findByPage(UserQueryForm queryForm) {
        return Result.ofSuccess(userService.findByPage(queryForm));
    }

    @PostMapping("/add")
    public Result<Long> add(@RequestBody UserInsertForm insertForm) {
        return Result.ofSuccess(userService.addUser(insertForm));
    }

    @GetMapping("/info")
    public Result<UserVo> findById(Long id) {
        return Result.ofSuccess(userService.findById(id));
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserUpdateForm updateForm) {
        userService.updateUser(updateForm);
        return Result.ofSuccess();
    }

    @GetMapping("/delete")
    public Result delete(Long id) {
        userService.deleteUser(id);
        return Result.ofSuccess();
    }

    @GetMapping("/deleteBatch")
    public Result delete(String ids) {
        userService.deleteUser(ids);
        return Result.ofSuccess();
    }

    /**
     * 查询用户角色
     * create by yangxk on 2019/3/4 13:47
     */
    @GetMapping("/role")
    public Result getUserRole(long userId) {
        return Result.ofSuccess(userService.findUserRole(userId));
    }

    /**
     * 设置用户角色
     * create by yangxk on 2019/3/4 13:47
     */
    @PostMapping("/setRole")
    public Result setRole(@Valid @RequestBody UserRoleForm userRoleForm) {
        userService.setUserRole(userRoleForm.getUserId(), userRoleForm.getRoleIds());
        return Result.ofSuccess();
    }

    /**
     * 更新当前登录用户信息
     * create by yangxk 2018/10/29 20:59
     */
    @PostMapping("/updateUserinfo")
    public Result updateUser(@RequestBody UserUpdateForm updateForm) {
        userService.updateSessionUser(updateForm);
        return Result.ofSuccess();
    }

    /**
     * 修改当前登录用户密码
     * create by yangxk 2018/10/29 21:34
     */
    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody UserPwdForm pwdForm) {
        userService.updateSessionUserPassword(pwdForm);
        return Result.ofSuccess();
    }

    @PostMapping("/uploadAvatar")
    public Result upload(long userId, @RequestParam("avatar") MultipartFile file) {
        if (file == null) {
            return Result.ofError(ResponseCode.ILLEGAL_PARAM);
        }

        try {
            InputStream in = file.getInputStream();
            return Result.ofSuccess(userService.uploadAvatar(userId, file.getOriginalFilename(), in));
        } catch (IOException e) {
            e.printStackTrace();
            throw new BizException(ResponseCode.UPLOAD_ERROR);
        }
    }


}
